package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.MemberDoctorDO;
import com.bootdo.wx.domain.MemberExtractDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberDoctorService;
import com.bootdo.wx.service.MemberUserService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-06-15 16:37:05
 */
 
@Controller
@RequestMapping("/wx/memberDoctor")
public class MemberDoctorController {
	@Autowired
	private MemberDoctorService memberDoctorService;
	@Autowired
	private MemberUserService memberUserService;
	
	@GetMapping()
	@RequiresPermissions("wx:memberDoctor:memberDoctor")
	String MemberDoctor(){
	    return "wx/memberDoctor/memberDoctor";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberDoctor:memberDoctor")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
		//查询列表数据
	    if(params.containsKey("name")&&params.get("name")!=null&&!"".equals(params.get("name").toString())){
	    	Query query = new Query(params);
			List<MemberDoctorDO> memberDoctorList = memberDoctorService.likeList(query);
			if(memberDoctorList.size()>0){
				for (MemberDoctorDO memberDoctorDO : memberDoctorList) {
					MemberUserDO memberUserDO = memberUserService.get(memberDoctorDO.getUserId());
					if(memberUserDO!=null){
						memberDoctorDO.setUserId(memberUserDO.getName());
					}
				}
			}
			int total = memberDoctorList.size();
			PageUtils pageUtils = new PageUtils(memberDoctorList, total);
			return pageUtils;
	    }
        Query query = new Query(params);
		List<MemberDoctorDO> memberDoctorList = memberDoctorService.list(query);
		if(memberDoctorList.size()>0){
			for (MemberDoctorDO memberDoctorDO : memberDoctorList) {
				MemberUserDO memberUserDO = memberUserService.get(memberDoctorDO.getUserId());
				if(memberUserDO!=null){
					memberDoctorDO.setUserId(memberUserDO.getName());
				}
			}
		}
		int total = memberDoctorService.count(query);
		PageUtils pageUtils = new PageUtils(memberDoctorList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberDoctor:add")
	String add(){
	    return "wx/memberDoctor/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberDoctor:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberDoctorDO memberDoctor = memberDoctorService.get(id);
		model.addAttribute("memberDoctor", memberDoctor);
	    return "wx/memberDoctor/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberDoctor:add")
	public R save( MemberDoctorDO memberDoctor){
	    memberDoctor.setId(UUIDUtils.getUUID());
	    memberDoctor.setDelFlag(0);
	    memberDoctor.setCaAt(new Date());
		if(memberDoctorService.save(memberDoctor)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberDoctor:edit")
	public R update( MemberDoctorDO memberDoctor){
		memberDoctor.setUpAt(new Date());
		memberDoctorService.update(memberDoctor);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberDoctor:remove")
	public R remove( String id){
		MemberDoctorDO memberDoctor = memberDoctorService.get(id);
		memberDoctor.setDelFlag(1);
		memberDoctor.setUpAt(new Date());
		if(memberDoctorService.update(memberDoctor)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberDoctor:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MemberDoctorDO memberDoctor = memberDoctorService.get(string);
				memberDoctor.setDelFlag(1);
				memberDoctor.setUpAt(new Date());
				memberDoctorService.update(memberDoctor);
			}
		}
	
		return R.ok();
	}
	/***
	 * 
	 * @author long
	 * 2020年6月15日下午5:51:21
	 * 方法释义:审核医生
	 * 参数:
	 *
	 */
	@ResponseBody
	@PostMapping("/shen")
	@RequiresPermissions("wx:memberDoctor:edit")
	public R shen(MemberDoctorDO memberDoctor){
		return memberDoctorService.shen(memberDoctor);
	}
	
}
